﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RobotJog.CApLinearJogModeApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.RobotJog;

public sealed class CApLinearJogModeApp
{
  private bool _isActive;
  private CUiLinearJogModeGVCursorMgr _graphicViewerCursorMgr = new CUiLinearJogModeGVCursorMgr();

  public bool IsActive
  {
    get => this._isActive;
    set
    {
      if (this._isActive == value)
        return;
      this._isActive = value;
      this.ExecuteLinearJogGraphicViewModeCommand();
      if (this._isActive)
        this.Activate();
      else
        this.Deactivate();
    }
  }

  public void Initialize()
  {
  }

  public void Uninitialize() => this.IsActive = false;

  private void ExecuteLinearJogGraphicViewModeCommand()
  {
    new TxCommandManager().ExecuteCommandByCommandProgId("GraphicViewerModes.ModeLinearJogCmd.1", (object) null, false);
  }

  private void Activate()
  {
    this._graphicViewerCursorMgr.Initialize();
    this._graphicViewerCursorMgr.SetCursor(CUiLinearJogModeGVCursorMgr.eCursorType.Default);
    this.RegisterToGraphicViewerEvents();
  }

  private void Deactivate()
  {
    this._graphicViewerCursorMgr.SetCursor(CUiLinearJogModeGVCursorMgr.eCursorType.Default);
    this._graphicViewerCursorMgr.Uninitialize();
    this.UnregisterToGraphicViewerEvents();
  }

  private void RegisterToGraphicViewerEvents()
  {
    TxGraphicViewer.MouseDown += new TxGraphicViewer_MouseEventHandler(this.TxGraphicViewerEx_MouseDown);
    TxGraphicViewer.MouseMove += new TxGraphicViewer_MouseEventHandler(this.TxGraphicViewer_MouseMove);
  }

  private void UnregisterToGraphicViewerEvents()
  {
    TxGraphicViewer.MouseDown -= new TxGraphicViewer_MouseEventHandler(this.TxGraphicViewerEx_MouseDown);
    TxGraphicViewer.MouseMove -= new TxGraphicViewer_MouseEventHandler(this.TxGraphicViewer_MouseMove);
  }

  private ITxRobot GetRobotFromPickedObject(ITxObject pickedObj)
  {
    switch (pickedObj)
    {
      case ITxRobot _:
        return pickedObj as ITxRobot;
      case null:
      case ITxComponent _:
        return (ITxRobot) null;
      default:
        ITxObject scope = TxEngineeringDataInternal.GetScope(pickedObj);
        if (scope is ITxRobot)
          return scope as ITxRobot;
        goto case null;
    }
  }

  private void TxGraphicViewerEx_MouseDown(object sender, TxGraphicViewer_MouseEventArgs e)
  {
    if (e.Button != MouseButtons.Left)
      return;
    if (this.GetRobotFromPickedObject(TxGraphicViewerEx.The.GetPickInfo(e.X, e.Y).Object) != null)
    {
      this._graphicViewerCursorMgr.SetCursor(CUiLinearJogModeGVCursorMgr.eCursorType.Default);
      e.Handled = true;
    }
    else
      this._graphicViewerCursorMgr.SetCursor(CUiLinearJogModeGVCursorMgr.eCursorType.Default);
  }

  private void TxGraphicViewer_MouseMove(object sender, TxGraphicViewer_MouseEventArgs e)
  {
    if (this.GetRobotFromPickedObject(TxGraphicViewerEx.The.GetPickInfo(e.X, e.Y).Object) != null)
      this._graphicViewerCursorMgr.SetCursor(CUiLinearJogModeGVCursorMgr.eCursorType.Active);
    else
      this._graphicViewerCursorMgr.SetCursor(CUiLinearJogModeGVCursorMgr.eCursorType.Default);
  }
}
